package year2022.month123;
public class PrintNum {
    private Byte lock = new Byte("0");

    public static void main(String[] args){
        PrintNum p = new PrintNum();
        p.demo();
    }

    public void demo() {
        PrintThread a = new PrintThread("a");
        PrintThread b = new PrintThread("b");

        a.start();
        b.start();
    }

    class PrintThread extends Thread {
        public PrintThread(String name) {
            this.setName(name);
        }

        @Override
        public void run() {
            synchronized (lock) {
                for (int i=1; i<=30; ) {
                    if (i%10 == 0) {
                        System.out.println();
                        try {
                            lock.wait();    //暂时释放资源
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.print(this.getName()+": "+i+"\t");
                    i++;
                    lock.notify();  //唤醒另外一个进程
                }
            }
        }
    }
}
